const express = require("express");
const router = express.Router();
const url = require("url");
const openUrl = require("../config/openUrl");

const login = require("./admin/login");
const register = require("./admin/register");
const main = require("./admin/main");

// 权限访问设置
router.use((req, res, next) => {
    let visitUrl = url.parse(req.url).pathname; // pathname 防止携带参数
    if (openUrl.indexOf(visitUrl) != -1 || req.session.adminInfo) {
        // 说明用户访问的是开放地址, 或者用户已经验证过
        next();
    } else {
        // 说明用户访问的是需要验证过后的地址, 并且未验证
        res.redirect(`/${req.app.locals.adminPath}/login`);
    }
});

router.use("/", main);
router.use("/login", login);
router.use("/register", register);

module.exports = router;